﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace Ex2.StudentsInfo
{
    public class StudentsInfo
    {
        public void AddStudent(Student newStudent, string fileName)
        {
            using (FileStream output = new FileStream(fileName, FileMode.Append))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(output, newStudent);
            }
        }

        public void PrintAllStudents(string fileName)
        {
            using (FileStream input = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                BinaryFormatter reader = new BinaryFormatter();
                
                while(input.Position<input.Length)
                {
                    Student currentStudent = (Student)reader.Deserialize(input);
                    Console.WriteLine(currentStudent);
                }
            }
        }

        public void DeleteStudentByFN(int fn,string fileName)
        {
            string tempFileName = "temp";
            File.Create(tempFileName).Dispose();

            using (FileStream input = new FileStream(fileName, FileMode.Open, FileAccess.Read),
                output = new FileStream(tempFileName, FileMode.Append))
            {
                BinaryFormatter reader = new BinaryFormatter();
                Dictionary<int, Student> dict = new Dictionary<int, Student>();

                while (input.Position < input.Length)
                {
                    Student currentStudent = (Student)reader.Deserialize(input);
                     dict.Add(currentStudent.FacultyNumber, currentStudent);
                }

                if (!dict.ContainsKey(fn))
                {
                    Console.WriteLine("Student with wanted Faculty Number doesn't exist!");
                }
                else
                {
                    dict.Remove(fn);
                }

                foreach (var student in dict.Values)
                {
                    reader.Serialize(output, student);
                }
                
            }
            File.Replace(tempFileName, fileName,"backUp.bak");
        }
    }

    public class StudentsInfoTest
    {
        public static string FILE_NAME = "..\\..\\studentsInfo.bin";
        public static void CreateFile(string fileName)
        {
            File.Create(fileName).Dispose();
        }
        public static void Main()
        {
            StudentsInfo info = new StudentsInfo();
            Student gosho = new Student("Gosho", "Petrov", 61250);
            Student pesho = new Student("Pesho", "Obreikov", 61325);
           
            //info.AddStudent(gosho, FILE_NAME);
            //info.AddStudent(pesho, FILE_NAME);
            
            info.PrintAllStudents(FILE_NAME);
            Console.WriteLine();

            //info.DeleteStudentByFN(61325, FILE_NAME);
            //info.PrintAllStudents(FILE_NAME);

        }
    }
}
